﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICeFramework.DataManagement.Interfaces;
using ICeFramework.DataManagement.Enums;
using Microsoft.SqlServer.Management.Smo;

namespace ICeFramework.DataManagement.MSSql
{
    public class MSSqlColumn : IColumn
    {
        #region public MSSqlColumn(Column column)
        /// <summary>
        /// Initializes a new instance of the <b>MSSqlColumn</b> class.
        /// </summary>
        /// <param name="column"></param>
        public MSSqlColumn(object parent, Column column)
        {
            _collation = column.Collation;
            _computed = column.Computed;
            _computedText = column.ComputedText;
            _dataType = new MSSqlDataType(column.DataType, column.Nullable);
            _default = column.Default;
            _id = column.ID;
            _identity = column.Identity;
            _identityIncrement = column.IdentityIncrement;
            _identitySeed = column.IdentitySeed;
            _inPrimaryKey = column.InPrimaryKey;
            _isForeignKey = column.IsForeignKey;
            _isFullTextIndexed = column.IsFullTextIndexed;
            _isPersisted = column.IsPersisted;
            _name = column.Name;
            _notForReplication = column.NotForReplication;
            _nullable = column.Nullable;
            _parent = parent;
            _rowGuidCol = column.RowGuidCol;
            _rule = column.Rule;
            _ruleSchema = column.RuleSchema;
            _readOnly = column.InPrimaryKey || column.DataType.SqlDataType == SqlDataType.Timestamp;
            
            switch (column.State)
            {
                case SqlSmoState.Creating:
                    _state = SqlObjectState.Creating;
                    break;
                case SqlSmoState.Dropped:
                    _state = SqlObjectState.Dropped;
                    break;
                case SqlSmoState.Existing:
                    _state = SqlObjectState.Existing;
                    break;
                case SqlSmoState.Pending:
                    _state = SqlObjectState.Pending;
                    break;
                case SqlSmoState.ToBeDropped:
                    _state = SqlObjectState.ToBeDropped;
                    break;
            }
        }
        #endregion

        #region public override string ToString()
        /// <summary>
        /// Returns a <see cref="string"/> that represents the current <see cref="ICeFramework.DataAccess.DataAccess.MSSql.MSSqlColumn"/>.
        /// </summary>
        /// <returns>A <see cref="string"/> that represents the current <see cref="ICeFramework.DataAccess.DataAccess.MSSql.MSSqlColumn"/>.</returns>
        public override string ToString()
        {
            return string.Format("{0} [{1}, {2}]", _name, string.Format("{0}{1}", _inPrimaryKey ? "PK, " : _isForeignKey ? "FK, " : "", _dataType.ToString()), _nullable ? "null" : "not null");
        }
        #endregion

        private string _collation;
        #region public string Collation
        /// <summary>
        /// Get/Sets the Collation of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public string Collation
        {
            get
            {
                return _collation;
            }
            set
            {
                _collation = value;
            }
        }
        #endregion

        private bool _computed;
        #region public bool Computed
        /// <summary>
        /// Gets the Computed of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public bool Computed
        {
            get
            {
                return _computed;
            }
        }
        #endregion

        private string _computedText;
        #region public string ComputedText
        /// <summary>
        /// Get/Sets the ComputedText of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public string ComputedText
        {
            get
            {
                return _computedText;
            }
            set
            {
                _computedText = value;
            }
        }
        #endregion

        private IDataType _dataType;
        #region public IDataType DataType
        /// <summary>
        /// Get/Sets the DataType of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public IDataType DataType
        {
            get
            {
                return _dataType;
            }
            set
            {
                _dataType = value;
            }
        }
        #endregion

        private string _default;
        #region public string Default
        /// <summary>
        /// Get/Sets the Default of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public string Default
        {
            get
            {
                return _default;
            }
            set
            {
                _default = value;
            }
        }
        #endregion

        private int _id;
        #region public string ID
        /// <summary>
        /// Gets the ID of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public int ID
        {
            get { return _id; }
        }
        #endregion

        private bool _identity;
        #region public bool Identity
        /// <summary>
        /// Get/Sets the Identity of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public bool Identity
        {
            get
            {
                return _identity;
            }
            set
            {
                _identity = value;
            }
        }
        #endregion

        private long _identityIncrement;
        #region public long IdentityIncrement
        /// <summary>
        /// Get/Sets the IdentityIncrement of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public long IdentityIncrement
        {
            get
            {
                return _identityIncrement;
            }
            set
            {
                _identityIncrement = value;
            }
        }
        #endregion

        private long _identitySeed;
        #region public long IdentitySeed
        /// <summary>
        /// Get/Sets the IdentitySeed of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public long IdentitySeed
        {
            get
            {
                return _identitySeed;
            }
            set
            {
                _identitySeed = value;
            }
        }
        #endregion

        private bool _inPrimaryKey;
        #region public bool InPrimaryKey
        /// <summary>
        /// Gets the InPrimaryKey of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public bool InPrimaryKey
        {
            get { return _inPrimaryKey; }
        }
        #endregion

        private bool _isForeignKey;
        #region public bool IsForeignKey
        /// <summary>
        /// Gets the IsForeignKey of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public bool IsForeignKey
        {
            get { return _isForeignKey; }
        }
        #endregion

        private bool _isFullTextIndexed;
        #region public bool IsFullTextIndexed
        /// <summary>
        /// Gets the IsFullTextIndexed of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public bool IsFullTextIndexed
        {
            get { return _isFullTextIndexed; }
        }
        #endregion

        private bool _isPersisted;
        #region public bool IsPersisted
        /// <summary>
        /// Gets the IsPersisted of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public bool IsPersisted
        {
            get { return _isPersisted; }
        }
        #endregion

        private string _name;
        #region public string Name
        /// <summary>
        /// Get/Sets the Name of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        #endregion

        private bool _notForReplication;
        #region public bool NotForReplication
        /// <summary>
        /// Gets the NotForReplication of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public bool NotForReplication
        {
            get { return _notForReplication; }
        }
        #endregion

        private bool _nullable;
        #region public bool Nullable
        /// <summary>
        /// Get/Sets the Nullable of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public bool Nullable
        {
            get
            {
                return _nullable;
            }
            set
            {
                _nullable = value;
            }
        }
        #endregion

        private object _parent;
        #region public object Parent
        /// <summary>
        /// Gets the Parent of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public object Parent
        {
            get { return _parent; }
        }
        #endregion

        private bool _rowGuidCol;
        #region public bool RowGuidCol
        /// <summary>
        /// Get/Sets the RowGuidCol of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public bool RowGuidCol
        {
            get
            {
                return _rowGuidCol;
            }
            set
            {
                _rowGuidCol = value;
            }
        }
        #endregion

        private string _rule;
        #region public string Rule
        /// <summary>
        /// Get/Sets the Rule of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public string Rule
        {
            get
            {
                return _rule;
            }
            set
            {
                _rule = value;
            }
        }
        #endregion

        private string _ruleSchema;
        #region public string RuleSchema
        /// <summary>
        /// Get/Sets the RuleSchema of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public string RuleSchema
        {
            get
            {
                return _ruleSchema;
            }
            set
            {
                _ruleSchema = value;
            }
        }
        #endregion

        private SqlObjectState _state;
        #region public SqlObjectState State
        /// <summary>
        /// Gets the State of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public SqlObjectState State
        {
            get { return _state; }
        }
        #endregion

        private bool _readOnly;
        #region public bool ReadOnly
        /// <summary>
        /// Gets the ReadOnly of the MSSqlColumn
        /// </summary>
        /// <value></value>
        public bool ReadOnly
        {
            get { return _readOnly; }
        }
        #endregion
    }
}
